package diskmgr;

import iterator.BPIterator;
import iterator.FileScan;
import iterator.FileScanException;
import iterator.FldSpec;
import iterator.InvalidRelation;
import iterator.Iterator;
import iterator.JoinsException;
import iterator.LowMemException;
import iterator.RelSpec;
import iterator.Sort;
import iterator.SortException;
import iterator.TupleUtilsException;
import iterator.UnknowAttrType;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

import org.w3c.dom.Attr;

import global.*;
import btree.*;
import bufmgr.*;
import heap.*;
import heap.FieldNumberOutOfBoundException;
import heap.FileAlreadyDeletedException;
import heap.HFBufMgrException;
import heap.HFDiskMgrException;
import heap.HFException;
import heap.InvalidSlotNumberException;
import heap.InvalidTupleSizeException;
import heap.InvalidTypeException;
import heap.SpaceNotAvailableException;
import tripleheap.*;
import tripleheap.InvalidTripleSizeException;

public class Stream extends BPIterator implements GlobalConst{

		
	//RdfDB rdfdatabase;
	//Heapfile tupleHeap;
	String heapFilename = "tupleHeap";
	Heapfile tmpHeap;
	Heapfile tupleHeap;
	//TripleHeapfile tripleHeap;
	Scan scan;
	

	public Stream(int orderType , String subjectFilter, String predicateFilter, String objectFilter,double confidenceFilter, int numbuf) 
	throws tripleheap.InvalidSlotNumberException, InvalidTripleSizeException, THFException, THFDiskMgrException, THFBufMgrException, Exception
	{
		//init(hf);
		tupleHeap = new Heapfile(null);
		//tmpHeap = new Heapfile("tmpHeap");
		searchQuery(subjectFilter, predicateFilter, objectFilter, confidenceFilter);
		//sortTupleHeap(numbuf, orderType);
		
		scan = tupleHeap.openScan();
		//sortHeap();
	}
	
	private void insertInTripleHeap() throws tripleheap.InvalidSlotNumberException, InvalidTripleSizeException, THFException, THFDiskMgrException, THFBufMgrException, Exception
	{
		Scan scan = tupleHeap.openScan();
		RID rid = new RID();
		while(true)
		{
			Tuple t =  scan.getNext(rid);
			AttrType[] attrType = new AttrType[4];
			
			attrType[0] = new AttrType(AttrType.attrString);
			attrType[1] = new AttrType(AttrType.attrString);
			attrType[2] = new AttrType(AttrType.attrString);
			//attrType[3] = new AttrType(AttrType.attrDouble);
			
			short[] strSize = new short[3];
			strSize[0] = 100;
			strSize[1] = 100;
			strSize[2] = 100;
			
			 //Tuple tuple = new Tuple();
			 t.setHdr((short)4, attrType, strSize);
			if(t == null) {
				break;
			}     
			else
			{
				String sub = t.getStrFld(1);
				sub = sub.trim();
				KeyClass key = new StringKey(sub);
				
				BTFileScan bscan = SystemDefs.subTIDBTIndex.new_scan(key, key);
				KeyDataEntry entry = bscan.get_next();
				if(entry != null)
				{
					LeafData ld = (LeafData)(entry.data);
					TID tid = ld.getData();
					Triple triple = SystemDefs.tripleHeap.getTriple(tid);
					byte[] data = triple.getTripleByteArray();
					SystemDefs.tripleHeapStream.insertTriple(data);
				}
				
			}
		}
	}
	
	public Tuple get_next() throws InvalidTupleSizeException, IOException
	{
		RID rid = new RID();
		while(true)
		{
			Tuple t = scan.getNext(rid);
			if(t == null)
				return null;
			return t;
		}
	}
	
	public void close() throws InvalidSlotNumberException, FileAlreadyDeletedException, InvalidTupleSizeException, HFBufMgrException, HFDiskMgrException, IOException
	{
		if(!closeFlag)
		{
			scan.closescan();
			tupleHeap.deleteFile();
			closeFlag = true;
		}
	}
	public Tuple get_next(RID rid) throws InvalidTripleSizeException, IOException, InvalidTupleSizeException
	{
		Tuple t = scan.getNext(rid);
		return t;
	}
	
	public void insertTupleHeap(String sub, String pred, String obj, double conf) throws FileScanException, TupleUtilsException, InvalidRelation, IOException, InvalidTypeException, InvalidTupleSizeException, FieldNumberOutOfBoundException, InvalidSlotNumberException, SpaceNotAvailableException, HFException, HFBufMgrException, HFDiskMgrException
	{
		AttrType[] attrType = new AttrType[4];
		
		attrType[0] = new AttrType(AttrType.attrString);
		attrType[1] = new AttrType(AttrType.attrString);
		attrType[2] = new AttrType(AttrType.attrString);
		//attrType[3] = new AttrType(AttrType.attrDouble);
		attrType[3] = new AttrType(AttrType.attrReal);
		
		short[] strSize = new short[3];
		strSize[0] = 100;
		strSize[1] = 100;
		strSize[2] = 100;
		
		 Tuple tuple = new Tuple();
		 tuple.setHdr((short)4, attrType, strSize);
		 tuple.setStrFld(1, sub);
		 tuple.setStrFld(2, pred);
		 tuple.setStrFld(3, obj);
		 //tuple.setDblFld(4, conf);
		 tuple.setFloFld(4, (float)conf);
		 
		 byte[] tupledata = tuple.getTupleByteArray();
		 tupleHeap.insertRecord(tupledata);
		
	}
	
	
	public void sortHeap(AttrType[] attrType, short[] strSize, FldSpec[] projlist, TupleOrder order, int fieldNum, int numbuf) throws UnknowAttrType, LowMemException, JoinsException, Exception
	{
		FileScan fscan = new FileScan(heapFilename, attrType, strSize, (short) 4, 4, projlist, null);
		Sort sort = new Sort(attrType, (short) 4, strSize, fscan, fieldNum, order, 100, numbuf);
		 Tuple t = sort.get_next();
		 SystemDefs.tupleHeap.deleteFile();
		 SystemDefs.tupleHeap = new Heapfile(heapFilename);
		 while(t!=null)
		 {
			 byte[] tdata = t.getTupleByteArray();
			 SystemDefs.tupleHeap.insertRecord(tdata);
			 t = sort.get_next();
		 }
		// SystemDefs.tupleHeap.deleteFile();
		 
		// SystemDefs.tupleHeap = tmpHeap;
		 //tmpHeap.deleteFile();
		 //tmpHeap = null;
		// SystemDefs.tupleHeap.deleteFile();
		// fscan.close();
		 sort.close();
	}
	
	public void sortTupleHeap(int numbuf, int orderType) throws UnknowAttrType, LowMemException, JoinsException, Exception
	{
		 FldSpec[] projlist = new FldSpec[4];
		 RelSpec rel = new RelSpec(RelSpec.outer); 
		 projlist[0] = new FldSpec(rel, 1);
		 projlist[1] = new FldSpec(rel, 2);
		 projlist[2] = new FldSpec(rel, 3);
		 projlist[3] = new FldSpec(rel, 4);
		 AttrType[] attrType = new AttrType[4];
		 attrType[0] = new AttrType(AttrType.attrString);
		 attrType[1] = new AttrType(AttrType.attrString);
		 attrType[2] = new AttrType(AttrType.attrString);
		 //attrType[3] = new AttrType(AttrType.attrDouble);
		 attrType[3] = new AttrType(AttrType.attrReal);
			
		 short[] strSize = new short[3];
		 strSize[0] = 100;
		 strSize[1] = 100;
		 strSize[2] = 100;
		 
		 TupleOrder order = new TupleOrder(TupleOrder.Ascending);
		 FileScan fscan;
		 
		 Sort sort; 
		 switch(orderType)
		 {
		 case 1:
			 sortHeap(attrType, strSize, projlist, order, 1, numbuf);
			 sortHeap(attrType, strSize, projlist, order, 2, numbuf);
			 sortHeap(attrType, strSize, projlist, order, 3, numbuf);
			 sortHeap(attrType, strSize, projlist, order, 4, numbuf);
			 break;
		 case 2:
			 sortHeap(attrType, strSize, projlist, order, 2, numbuf);
			 sortHeap(attrType, strSize, projlist, order, 1, numbuf);
			 sortHeap(attrType, strSize, projlist, order, 3, numbuf);
			 sortHeap(attrType, strSize, projlist, order, 4, numbuf);
			 break;
		 case 3:
			 sortHeap(attrType, strSize, projlist, order, 1, numbuf);
			 sortHeap(attrType, strSize, projlist, order, 4, numbuf);
			 break;
		 case 4:
			 sortHeap(attrType, strSize, projlist, order, 2, numbuf);
			 sortHeap(attrType, strSize, projlist, order, 4, numbuf);
			 break;
		 case 5:
			 sortHeap(attrType, strSize, projlist, order, 3, numbuf);	
			 break;
		 case 6:
			 sortHeap(attrType, strSize, projlist, order, 4, numbuf);
			 break;
		 default: 
			 break;
		 
		 }
		 
			Scan tmpScan = SystemDefs.tupleHeap.openScan();
			RID ridtmp = new RID();
			Tuple tmpTuple = tmpScan.getNext(ridtmp);
			while(tmpTuple != null)
			{
				tmpTuple.setHdr((short)4, attrType, strSize);
				String sub = tmpTuple.getStrFld(1);
				String pred = tmpTuple.getStrFld(2);
				String obj = tmpTuple.getStrFld(3);
				//double conf = tmpTuple.getDblFld(4);
				float conf = tmpTuple.getFloFld(4);
				System.out.println(sub + "\t\t"+ pred +"\t\t"+obj + "\t\t"+conf);
				tmpTuple = tmpScan.getNext(ridtmp);
			}
			tmpScan.closescan();
			SystemDefs.tupleHeap.deleteFile();
			System.out.println("\n\n\n");
	}
	
		
	public void searchQuery(String subject, String predicate, String object, double confidence) throws InvalidSlotNumberException, InvalidTripleSizeException, THFException, THFDiskMgrException, THFBufMgrException, Exception
	{
		boolean subFlag = true;
		boolean predFlag = true;
		boolean objFlag = true;
		boolean confFlag = true;
		
		if(subject.equals("0"))
			subFlag = false;
		if(predicate.equals("0"))
			predFlag = false;
		if(object.equals("0"))
			objFlag = false;
		if(confidence == 0)
			confFlag = false;
		
		
		if(subFlag && predFlag && objFlag && confFlag)
		{
			String compSubPred = subject + ","+ predicate;
			KeyClass key = new StringKey(compSubPred);
			BTFileScan scan = SystemDefs.compTID_SP_Index.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			ArrayList<String> tList = new ArrayList<String>();
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				String tidStr = tid.pageNo.pid + ","+tid.slotNo;
				tList.add(tidStr);
				entry = scan.get_next();
			}
			
			String compObjConf = object + ","+ confidence;
			key = new StringKey(compObjConf);
			scan = SystemDefs.compTID_OC_Index.new_scan(key, key);
			entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				String tidStr = tid.pageNo.pid +","+tid.slotNo;
				if(tList.contains(tidStr))
				{
										
					insertTupleHeap(subject, predicate, object, (float)confidence);
					
				}
				entry = scan.get_next();
			}
			
		}
		else if(subFlag && predFlag && objFlag && !confFlag)
		{
			String compSubPred = subject + ","+ predicate;
			KeyClass key = new StringKey(compSubPred);
			BTFileScan scan = SystemDefs.compTID_SP_Index.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			
			ArrayList<String> tList = new ArrayList<String>();
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				String tidStr = tid.pageNo.pid +","+ tid.slotNo;
				tList.add(tidStr);
				entry = scan.get_next();
			}
			
			String compPredObj = predicate +","+object;
			key = new StringKey(compPredObj);
			scan = SystemDefs.compTID_PO_Index.new_scan(key, key);
			entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				String tidStr = tid.pageNo.pid +","+ tid.slotNo;
				if(tList.contains(tidStr))
				{
					Triple triple = SystemDefs.tripleHeap.getTriple(tid);
					byte[] tdata = triple.getTripleByteArray();
					
					double conf = Convert.getDblValue(24, tdata);
					insertTupleHeap(subject, predicate, object, (float)conf);
					
					
				}
				entry = scan.get_next();
			}
		}
		else if(subFlag && predFlag && !objFlag && confFlag)
		{
			String compSubPred = subject + ","+ predicate;
			KeyClass key = new StringKey(compSubPred);
			BTFileScan scan = SystemDefs.compTID_SP_Index.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			ArrayList<String> tList = new ArrayList<String>();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				String tidStr = tid.pageNo.pid +","+ tid.slotNo;
				tList.add(tidStr);
				entry = scan.get_next();
			}
			
			String compPredConf = predicate +","+confidence;
			key = new StringKey(compPredConf);
			scan = SystemDefs.compTID_PC_Index.new_scan(key, key);
			entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				String tidStr = tid.pageNo.pid +","+ tid.slotNo;
				if(tList.contains(tidStr))
				{
					Triple triple = SystemDefs.tripleHeap.getTriple(tid);
					byte[] tdata = triple.getTripleByteArray();
					
					PageId pageId = new PageId();
					pageId.pid = Convert.getIntValue(16, tdata);
					int slotno = Convert.getIntValue(20, tdata);
					LID lid = new LID(pageId,slotno);
					String obj = SystemDefs.entityLabelHeap.getLabel(lid);
					insertTupleHeap(subject, predicate, obj, (float)confidence);
				}
				entry = scan.get_next();
			}
		}
		else if(subFlag && predFlag && !objFlag && !confFlag)
		{
			String compSubPred = subject + ","+ predicate;
			KeyClass key = new StringKey(compSubPred);
			BTFileScan scan = SystemDefs.compTID_SP_Index.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				
				Triple triple = SystemDefs.tripleHeap.getTriple(tid);
				byte[] tdata = triple.getTripleByteArray();
				
				PageId pageId = new PageId();
				pageId.pid = Convert.getIntValue(16, tdata);
				int slotno = Convert.getIntValue(20, tdata);
				LID lid = new LID(pageId,slotno);
				String obj = SystemDefs.entityLabelHeap.getLabel(lid);
				
				double conf = Convert.getDblValue(24, tdata);
				insertTupleHeap(subject, predicate, obj, (float)conf);
				entry = scan.get_next();
			}
		}
		else if(subFlag && !predFlag && objFlag && confFlag)
		{
			String compSubObj = subject + ","+ object;
			KeyClass key = new StringKey(compSubObj);
			BTFileScan scan = SystemDefs.compTID_SO_Index.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			ArrayList<String> tList = new ArrayList<String>();
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				String tidStr = tid.pageNo.pid +","+ tid.slotNo;
				tList.add(tidStr);
				entry = scan.get_next();
			}
			
			String compObjConf = object +","+confidence;
			key = new StringKey(compObjConf);
			scan = SystemDefs.compTID_OC_Index.new_scan(key, key);
			entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				String tidStr = tid.pageNo.pid +","+ tid.slotNo;
				if(tList.contains(tidStr))
				{
					Triple triple = SystemDefs.tripleHeap.getTriple(tid);
					byte[] tdata = triple.getTripleByteArray();
					
					PageId pageId = new PageId();
					pageId.pid = Convert.getIntValue(8, tdata);
					int slotno = Convert.getIntValue(12, tdata);
					LID lid = new LID(pageId,slotno);
					String pred = SystemDefs.predicateLabelHeap.getLabel(lid);
					insertTupleHeap(subject, pred, object, (float)confidence);
				}
				entry = scan.get_next();
			}
		}
		else if(subFlag && !predFlag && objFlag && !confFlag)
		{
			String compSubObj = subject + ","+ object;
			KeyClass key = new StringKey(compSubObj);
			BTFileScan scan = SystemDefs.compTID_SO_Index.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				Triple triple = SystemDefs.tripleHeap.getTriple(tid);
				byte[] tdata = triple.getTripleByteArray();
				
				PageId pageId = new PageId();
				pageId.pid = Convert.getIntValue(8, tdata);
				int slotno = Convert.getIntValue(12, tdata);
				LID lid = new LID(pageId,slotno);
				String pred = SystemDefs.predicateLabelHeap.getLabel(lid);
				
				double conf = Convert.getDblValue(24, tdata);
				insertTupleHeap(subject, pred, object,(float) conf);
				entry = scan.get_next();
			}
			
			
		}
		else if(subFlag && !predFlag && !objFlag && confFlag)
		{
			String compSubConf = subject + ","+ confidence;
			KeyClass key = new StringKey(compSubConf);
			BTFileScan scan = SystemDefs.compTID_SC_Index.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				Triple triple = SystemDefs.tripleHeap.getTriple(tid);
				byte[] tdata = triple.getTripleByteArray();
				
				PageId pageId = new PageId();
				pageId.pid = Convert.getIntValue(8, tdata);
				int slotno = Convert.getIntValue(12, tdata);
				LID lid = new LID(pageId,slotno);
				String pred = SystemDefs.predicateLabelHeap.getLabel(lid);
				
				pageId = new PageId();
				pageId.pid = Convert.getIntValue(16, tdata);
				slotno = Convert.getIntValue(24, tdata);
				lid = new LID(pageId,slotno);
				String obj = SystemDefs.entityLabelHeap.getLabel(lid);
				insertTupleHeap(subject, pred, obj, (float)confidence);
				entry = scan.get_next();
			}
		}
		else if(subFlag && !predFlag && !objFlag && !confFlag)
		{
			
			KeyClass key = new StringKey(subject);
			BTFileScan scan = SystemDefs.subTIDBTIndex.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				Triple triple = SystemDefs.tripleHeap.getTriple(tid);
				byte[] tdata = triple.getTripleByteArray();
				
				PageId pageId = new PageId();
				pageId.pid = Convert.getIntValue(8, tdata);
				int slotno = Convert.getIntValue(12, tdata);
				LID lid = new LID(pageId,slotno);
				String pred = SystemDefs.predicateLabelHeap.getLabel(lid);
				
				pageId = new PageId();
				pageId.pid = Convert.getIntValue(16, tdata);
				slotno = Convert.getIntValue(20, tdata);
				lid = new LID(pageId,slotno);
				String obj = SystemDefs.entityLabelHeap.getLabel(lid);
				
				double conf = Convert.getDblValue(24, tdata);
				insertTupleHeap(subject, pred, obj,(float) conf);
				entry = scan.get_next();
			}
		}
		else if(!subFlag && predFlag && objFlag && confFlag)
		{
			String compPredObj = predicate + ","+ object;
			KeyClass key = new StringKey(compPredObj);
			BTFileScan scan = SystemDefs.compTID_PO_Index.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			ArrayList<String> tList = new ArrayList<String>();
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				String tidStr = tid.pageNo.pid +","+ tid.slotNo;
				tList.add(tidStr);
				entry = scan.get_next();
			}
			
			String compObjConf = object +","+confidence;
			key = new StringKey(compObjConf);
			scan = SystemDefs.compTID_OC_Index.new_scan(key, key);
			entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				String tidStr = tid.pageNo.pid +","+ tid.slotNo;
				if(tList.contains(tidStr))
				{
					Triple triple = SystemDefs.tripleHeap.getTriple(tid);
					byte[] tdata = triple.getTripleByteArray();
					
					PageId pageId = new PageId();
					pageId.pid = Convert.getIntValue(0, tdata);
					int slotno = Convert.getIntValue(4, tdata);
					LID lid = new LID(pageId,slotno);
					String sub = SystemDefs.entityLabelHeap.getLabel(lid);
					
					insertTupleHeap(sub, predicate, object, (float)confidence);
					entry = scan.get_next();
				}
			}
		}
		else if(!subFlag && predFlag && objFlag && !confFlag)
		{
			String compPredObj = predicate + ","+ object;
			KeyClass key = new StringKey(compPredObj);
			BTFileScan scan = SystemDefs.compTID_PO_Index.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				Triple triple = SystemDefs.tripleHeap.getTriple(tid);
				byte[] tdata = triple.getTripleByteArray();
				
				PageId pageId = new PageId();
				pageId.pid = Convert.getIntValue(0, tdata);
				int slotno = Convert.getIntValue(4, tdata);
				LID lid = new LID(pageId,slotno);
				String sub = SystemDefs.entityLabelHeap.getLabel(lid);
				
				double conf = Convert.getDblValue(24, tdata);
				insertTupleHeap(sub, predicate, object,(float) conf);
				entry = scan.get_next();
			}
			
			
		}
		else if(!subFlag && predFlag && !objFlag && confFlag)
		{
			String compPredConf = predicate + ","+ confidence;
			KeyClass key = new StringKey(compPredConf);
			BTFileScan scan = SystemDefs.compTID_PC_Index.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				Triple triple = SystemDefs.tripleHeap.getTriple(tid);
				byte[] tdata = triple.getTripleByteArray();
				
				PageId pageId = new PageId();
				pageId.pid = Convert.getIntValue(0, tdata);
				int slotno = Convert.getIntValue(4, tdata);
				LID lid = new LID(pageId,slotno);
				String sub = SystemDefs.entityLabelHeap.getLabel(lid);
				
				pageId = new PageId();
				pageId.pid = Convert.getIntValue(16, tdata);
				slotno = Convert.getIntValue(20, tdata);
				lid = new LID(pageId,slotno);
				String obj = SystemDefs.entityLabelHeap.getLabel(lid);
				insertTupleHeap(sub, predicate, obj, (float)confidence);
				entry = scan.get_next();
			}
		}
		else if(!subFlag && predFlag && !objFlag && !confFlag)
		{
			
			KeyClass key = new StringKey(predicate);
			BTFileScan scan = SystemDefs.predTIDBTIndex.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				Triple triple = SystemDefs.tripleHeap.getTriple(tid);
				byte[] tdata = triple.getTripleByteArray();
				
				PageId pageId = new PageId();
				pageId.pid = Convert.getIntValue(0, tdata);
				int slotno = Convert.getIntValue(4, tdata);
				LID lid = new LID(pageId,slotno);
				String sub = SystemDefs.entityLabelHeap.getLabel(lid);
				
				pageId = new PageId();
				pageId.pid = Convert.getIntValue(16, tdata);
				slotno = Convert.getIntValue(20, tdata);
				lid = new LID(pageId,slotno);
				String obj = SystemDefs.entityLabelHeap.getLabel(lid);
				double conf = Convert.getDblValue(24, tdata);
				insertTupleHeap(sub, predicate, obj, (float)conf);
				entry = scan.get_next();
			}
		}
		else if(!subFlag && !predFlag && objFlag && confFlag)
		{
			String compObjConf = object +","+ confidence;
			KeyClass key = new StringKey(compObjConf);
			BTFileScan scan = SystemDefs.compTID_OC_Index.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				Triple triple = SystemDefs.tripleHeap.getTriple(tid);
				byte[] tdata = triple.getTripleByteArray();
				
				PageId pageId = new PageId();
				pageId.pid = Convert.getIntValue(0, tdata);
				int slotno = Convert.getIntValue(4, tdata);
				LID lid = new LID(pageId,slotno);
				String sub = SystemDefs.entityLabelHeap.getLabel(lid);
				
				pageId = new PageId();
				pageId.pid = Convert.getIntValue(8, tdata);
				slotno = Convert.getIntValue(12, tdata);
				lid = new LID(pageId,slotno);
				String pred = SystemDefs.predicateLabelHeap.getLabel(lid);
				insertTupleHeap(sub, pred, object,(float) confidence);
				entry = scan.get_next();
			}
		}
		else if(!subFlag && !predFlag && objFlag && !confFlag)
		{
			
			KeyClass key = new StringKey(object);
			BTFileScan scan = SystemDefs.objTIDBTIndex.new_scan(key, key);
			KeyDataEntry entry = scan.get_next();
			
			while(entry!=null)
			{
				LeafData ld = (LeafData)(entry.data);
				TID tid = ld.getData();
				Triple triple = SystemDefs.tripleHeap.getTriple(tid);
				byte[] tdata = triple.getTripleByteArray();
				
				PageId pageId = new PageId();
				pageId.pid = Convert.getIntValue(0, tdata);
				int slotno = Convert.getIntValue(4, tdata);
				LID lid = new LID(pageId,slotno);
				String sub = SystemDefs.entityLabelHeap.getLabel(lid);
				
				pageId = new PageId();
				pageId.pid = Convert.getIntValue(8, tdata);
				slotno = Convert.getIntValue(12, tdata);
				lid = new LID(pageId,slotno);
				String pred = SystemDefs.predicateLabelHeap.getLabel(lid);
				
				double conf = Convert.getDblValue(24, tdata);
				insertTupleHeap(sub, pred, object, (float)conf);
				entry = scan.get_next();
			}
		}
//		else if(!subFlag && !predFlag && !objFlag && confFlag)
//		{
//			KeyClass key = new DoubleKey(confidence);
//			BTFileScan scan = SystemDefs.confTIDBTIndex.new_scan(key, key);
//			KeyDataEntry entry = scan.get_next();
//			
//			while(entry!=null)
//			{
//				LeafData ld = (LeafData)(entry.data);
//				TID tid = ld.getData();
//				Triple triple = SystemDefs.tripleHeap.getTriple(tid);
//				byte[] tdata = triple.getTripleByteArray();
//				
//				PageId pageId = new PageId();
//				pageId.pid = Convert.getIntValue(0, tdata);
//				int slotno = Convert.getIntValue(4, tdata);
//				LID lid = new LID(pageId,slotno);
//				String sub = SystemDefs.entityLabelHeap.getLabel(lid);
//				
//				pageId = new PageId();
//				pageId.pid = Convert.getIntValue(8, tdata);
//				slotno = Convert.getIntValue(12, tdata);
//				lid = new LID(pageId,slotno);
//				String pred = SystemDefs.predicateLabelHeap.getLabel(lid);
//				
//				pageId = new PageId();
//				pageId.pid = Convert.getIntValue(16, tdata);
//				slotno = Convert.getIntValue(20, tdata);
//				lid = new LID(pageId,slotno);
//				String obj = SystemDefs.entityLabelHeap.getLabel(lid);
//				
//				insertTupleHeap(sub, pred, obj,(float) confidence);
//				entry = scan.get_next();
//				
//			}
//		}
		else
		{
			//KeyClass key = new StringKey(subject);
			//BTFileScan scan = SystemDefs.subTIDBTIndex.new_scan(null, null);
			//KeyDataEntry entry = scan.get_next();
			TScan tscan = SystemDefs.tripleHeap.openScan();
			TID tid = new TID();
			Triple triple = tscan.getNext(tid);
			
			while(triple!=null)
			{
				byte[] tdata = triple.getTripleByteArray();
				
				PageId pageId = new PageId();
				pageId.pid = Convert.getIntValue(0, tdata);
				int slotno = Convert.getIntValue(4, tdata);
				LID lid = new LID(pageId,slotno);
				String sub = SystemDefs.entityLabelHeap.getLabel(lid);
				
				pageId = new PageId();
				pageId.pid = Convert.getIntValue(8, tdata);
				slotno = Convert.getIntValue(12, tdata);
				lid = new LID(pageId,slotno);
				String pred = SystemDefs.predicateLabelHeap.getLabel(lid);
				
				pageId = new PageId();
				pageId.pid = Convert.getIntValue(16, tdata);
				slotno = Convert.getIntValue(20, tdata);
				lid = new LID(pageId,slotno);
				String obj = SystemDefs.entityLabelHeap.getLabel(lid);
				
				double conf = Convert.getDblValue(24, tdata);
				
				insertTupleHeap(sub, pred, obj,(float) conf);
				triple = tscan.getNext(tid);
				
			}
		}
		
		
	}


}
